package com.lehecai.ucenter.action.server;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.lehecai.ucenter.action.BaseAction;
import com.lehecai.ucenter.entity.app.App;
import com.lehecai.ucenter.service.app.AppService;
import com.lehecai.ucenter.utils.AuthorizeSignUtils;

public class AuthorizeAction extends BaseAction {
	
	private static final long serialVersionUID = -8634484692576627642L;
	
	private AppService appService;
	
	private String token;
	private String random;
	private String sign;
	
	private String appName;
	
	private String callback;//成功后回调地址
    private String errMessage;
	
	public String handle() {
		if (StringUtils.isEmpty(token) || StringUtils.isEmpty(random) || StringUtils.isEmpty(sign) || StringUtils.isEmpty(callback)) {
			logger.error("授权认证参数不全！");
			super.setErrorMessage("授权认证参数不全！");
			return "authorizeFailure";
		}
		App app = appService.findByToken(token);
		if (app == null) {
			logger.error("您所访问系统未经过用户中心授权！");
			super.setErrorMessage("您所访问系统未经过用户中心授权！");
			return "authorizeFailure";
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("token", token);
		params.put("random", random);
		params.put("callback", callback);
		if (!AuthorizeSignUtils.generateAuthorizeSign(params, app.getSecret()).equals(sign)) {
			logger.error("授权签名错误！");
			super.setErrorMessage("授权签名错误！");
			return "authorizeFailure";
		}
		appName = app.getName();
        String msg = "";
        if (StringUtils.isNotEmpty(errMessage)) {
            Map<String, String> errorMessageMap = getErrorMessageMap();
            if (errorMessageMap != null && errorMessageMap.size() != 0) {
                msg = errorMessageMap.get(errMessage);
                logger.error(msg);
                super.setErrorMessage(msg);
            }
        }
        logger.info("通过认证，返回登录页面");
		return "login";
	}

    private Map<String, String> getErrorMessageMap() {
        Map<String, String> errorMessageMap = new HashMap<String, String>();
        errorMessageMap.put("1", "用户名不能为空");
        errorMessageMap.put("2", "密码不能为空");
        errorMessageMap.put("3", "验证码不能为空");
        errorMessageMap.put("4", "验证码错误");
        errorMessageMap.put("5", "用户名或密码错误");
        return errorMessageMap;
    }

	public AppService getAppService() {
		return appService;
	}

	public void setAppService(AppService appService) {
		this.appService = appService;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

    public String getErrMessage() {
        return errMessage;
    }

    public void setErrMessage(String errMessage) {
        this.errMessage = errMessage;
    }
}
